//
//  IMManager.m
//  MiAiApp
//
//  Created by voidcat on 2017/6/5.
//  Copyright © 2017年 voidcat. All rights reserved.
//

#import "IMManager.h"
#import <ImSDK/ImSDK.h>
#import <IMGroupExt/IMGroupExt.h>
#import "IMAGroup.h"

//@interface IMManager()<NIMLoginManagerDelegate,NIMChatManagerDelegate>
@interface IMManager()

@end

@implementation IMManager

SINGLETON_FOR_CLASS(IMManager);

#pragma mark ————— 初始化IM —————
-(void)initIM{
    
    IMALoginParam *loginParam = [[IMALoginParam alloc] init];
    [IMAPlatform configWith:loginParam.config];

    
    //在注册 NIMSDK appKey 之前先进行配置信息的注册，如是否使用新路径,是否要忽略某些通知，是否需要多端同步未读数
//    self.sdkConfigDelegate = [[NTESSDKConfigDelegate alloc] init];
//    [[NIMSDKConfig sharedConfig] setDelegate:self.sdkConfigDelegate];
//    [[NIMSDKConfig sharedConfig] setShouldSyncUnreadCount:YES];
//    [[NIMSDKConfig sharedConfig] setMaxAutoLoginRetryTimes:10];
//
//    [[[NIMSDK sharedSDK] loginManager] addDelegate:self];
//    [[[NIMSDK sharedSDK] chatManager] addDelegate:self];
//
//    [[NIMSDK sharedSDK] registerWithAppID:kIMAppKey
//                                  cerName:kIMPushCerName];
}

#pragma mark ————— IM登录 —————
-(void)IMLogin:(NSString *)IMID IMPwd:(NSString *)IMPwd completion:(loginBlock)completion{
    
    TIMLoginParam * login_param = [[TIMLoginParam alloc ]init];
    
    // identifier为用户名，userSig 为用户登录凭证
    // appidAt3rd 在私有帐号情况下，填写与sdkAppId 一样
    login_param.identifier = curUser.id;
//    login_param.userSig = curUser.UserSig;
    login_param.userSig = curUser.user_sign;

    login_param.appidAt3rd = KGlobalVariable.appmodel.sdkappid;
    
    [[TIMManager sharedInstance] login: login_param succ:^(){
        NSLog(@"Login Succ");
        [self enterBigGroup];
        completion(YES,nil);
//        [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"IM登录成功")];
    } fail:^(int code, NSString * err) {
//        if(curUser.id != nil)
//            [HUDHelper alert:[NSString stringWithFormat:ASLocalizedString(@"聊天登录失败%@"),err]];
        //登录失败重置用户信息
//        userManager.curUserInfo = nil;

        if(curUser != nil)
        {
            [[HUDHelper sharedInstance] tipMessage:err delay:0.3] ;
        }

        userManager.isLogined = NO;
        completion(NO,err);
        NSLog(@"Login Failed: %d->%@", code, err);
    }];
    
//    [[[NIMSDK sharedSDK] loginManager] login:IMID token:IMPwd completion:^(NSError * _Nullable error) {
//        if (!error) {
//            if (completion) {
//                completion(YES,nil);
//            }
//        }else{
//            if (completion) {
//                completion(NO,error.localizedDescription);
//            }
//        }
//    }];
}

#pragma mark 加入大群
- (void)enterBigGroup
{
    if (StrValid(KGlobalVariable.appmodel.group_id))
    {
//        [[TIMGroupManager sharedInstance] JoinGroup:KGlobalVariable.appmodel.group_id msg:nil succ:^{
//            NSLog(ASLocalizedString(@"加入全员广播大群成功"));
//        } fail:^(int code, NSString *msg) {
//            NSLog(ASLocalizedString(@"加入全员广播大群失败，错误码：%d，错误原因：%@"),code,msg);
//        }];
        
        [[TIMGroupManager sharedInstance] joinGroup:KGlobalVariable.appmodel.group_id msg:nil succ:^{
             NSLog(ASLocalizedString(@"加入全员广播大群成功"));
        } fail:^(int code, NSString *msg) {
            NSLog(ASLocalizedString(@"加入全员广播大群失败，错误码：%d，错误原因：%@"),code,msg);
        }];
    }
}

#pragma mark ————— IM退出 —————
-(void)IMLogout{
    
//    __weak IMAPlatform *ws = self;
    
    
    [[TIMGroupManager sharedInstance] quitGroup:KGlobalVariable.appmodel.group_id succ:^{
        DLog(@"IM 退出成功");
    } fail:^(int code, NSString *msg) {
        NSLog(ASLocalizedString(@"退出在线用户大群失败，错误码：%d，错误原因：%@"),code,msg);
    }];
    
    
    
//    [[[NIMSDK sharedSDK] loginManager] logout:^(NSError * _Nullable error) {
//        if (!error) {
//            DLog("IM 退出成功");
//        }else{
//            DLog("IM 退出失败 %@",error.localizedDescription);
//        }
//    }];
}

//-(void)onKick:(NIMKickReason)code clientType:(NIMLoginClientType)clientType
//{
//    NSString *reason = ASLocalizedString(@"你被踢下线");
//    switch (code) {
//            case NIMKickReasonByClient:
//            case NIMKickReasonByClientManually:{
//                reason = ASLocalizedString(@"你的帐号被踢出下线，请注意帐号信息安全");
//                break;
//            }
//            case NIMKickReasonByServer:
//            reason = ASLocalizedString(@"你被服务器踢下线");
//            break;
//        default:
//            break;
//    }
//    KPostNotification(KNotificationOnKick, nil);
//}
//
//#pragma mark ————— 代理 收到新消息 —————
//- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages{
//    DLog(ASLocalizedString(@"收到新消息"));
//}
//

@end
